package down.xiaoheihe.work.Controller;

import down.xiaoheihe.work.Entity.*;
import down.xiaoheihe.work.Mapper.TOrderMapper;
import down.xiaoheihe.work.Mapper.TProductMapper;
import down.xiaoheihe.work.Mapper.TimagesMapper;
import down.xiaoheihe.work.Mapper.UserMapper;
import down.xiaoheihe.work.utils.ElasticUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@MapperScan("down.xiaoheihe.work.Mapper")
@CrossOrigin
public class AdminController {
    @Autowired
    UserMapper tUserMapper;
    @Autowired
    TProductMapper tProductMapper;
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    ElasticUtils utils;

    @GetMapping("/getallusers")
    public List<User> 获取所有用户() {
        List<User> users = tUserMapper.selectByExample(new UserExample());
        return users;
    }

    @PatchMapping("/banned_user")
    public String 封禁用户(@RequestBody User user) {
        user.setIsDelete(1);
        tUserMapper.updateByPrimaryKeySelective(user);
        return "OK";
    }

    @PatchMapping("/unlock_user")
    public String 解封用户(@RequestBody User user) {
        user.setIsDelete(0);
        tUserMapper.updateByPrimaryKeySelective(user);
        return "OK";
    }

    @PatchMapping("/take_down")
    public String 下架商品(@RequestBody TProduct tProduct) throws IOException {
        Integer discontinuedStatus = 2;
        tProduct.setStatus(discontinuedStatus);
        tProductMapper.updateByPrimaryKeySelective(tProduct);
        utils.updateProducts(tProduct);
        return "OK";
    }

    @PatchMapping("/take_up")
    public String 商品重新上架(@RequestBody TProduct tProduct) throws IOException {
        Integer discontinuedStatus = 1;
        tProduct.setStatus(discontinuedStatus);
        tProductMapper.updateByPrimaryKeySelective(tProduct);
        utils.updateProducts(tProduct);
        return "OK";
    }

    @GetMapping("/getallgoods")
    public List<TProduct> 获取所有商品() {
        List<TProduct> list = tProductMapper.selectByExample(new TProductExample());
        return list;
    }

    @GetMapping("/getallorders")
    public List<TOrder> 获取所有订单() {
        List<TOrder> orderList = tOrderMapper.selectByExample(new TOrderExample());
        return orderList;
    }

    @PatchMapping("/deleteorder")
    public String 完成订单(@RequestBody TOrder order) {
        order.setStatus(5);
        tOrderMapper.updateByPrimaryKeySelective(order);
        return "OK";
    }

    /**
     * 数据总览
     *
     * @return
     */
    @GetMapping("/dataraddar")
    public Map getOverallData() {

        //最近30天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        calendar.add(Calendar.MONTH, -1);
        Date monthAgo = calendar.getTime();

        List<Long> res = new ArrayList<>();
        List<User> users = tUserMapper.selectByExample(new UserExample());


        List<TOrder> tOrders = tOrderMapper.selectByExample(new TOrderExample());
        //用户总数
        long userTotal = 0;
        //总交易量
        long transNumbers = 0;
        //总交易金额
        long moneyTotal = 0;
        //最近一个月订单数
        long monthOrders = 0;
        for (TOrder order : tOrders) {

            transNumbers++;
            Integer productId = order.getProductId();
            Integer guarentee = tProductMapper.selectByPrimaryKey(productId).getGuarentee();
            moneyTotal += (order.getTotalPrice() - guarentee);
            if (order.getCreateTime()!=null){
                if (monthAgo.compareTo(order.getCreateTime()) < 0) {
                    monthOrders++;
                }
            }

        }
        //总商品数量
        long productTotal = 0;
        List<TProduct> tProducts = tProductMapper.selectByExample(new TProductExample());
        for (TProduct product:
             tProducts) {
            productTotal++;
        }
        //月活
        long monthLogin = 0;
        for (User user : users) {
            userTotal++;
            if (user.getLatestLogin()!=null){
                if (monthAgo.compareTo(user.getLatestLogin()) < 0) {
                    monthLogin++;
                }
            }
        }
        res.add(userTotal);
        res.add(transNumbers);
        res.add(moneyTotal);
        res.add(monthOrders);
        res.add(productTotal);
        res.add(monthLogin);
        return Map.of("newchart", res);
    }


}
